package com.pubinfo.passbook.common.service.master.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pubinfo.passbook.common.entity.TRabbitmqPerson;
import com.pubinfo.passbook.common.entity.TWriteoffDataUser;
import com.pubinfo.passbook.common.mapper.TWriteoffDataUserMapper;
import com.pubinfo.passbook.common.model.bo.rabbitmq.TbPerRLOrgRel;
import com.pubinfo.passbook.common.model.dto.system.PersonListParam;
import com.pubinfo.passbook.common.model.http.request.GetWriteoffBaseData.GetWriteoffBaseDataItems;
import com.pubinfo.passbook.common.model.http.response.GetWriteoffBaseData.WriteoffBaseDataItem;
import com.pubinfo.passbook.common.model.http.response.GetWriteoffBaseData.WriteoffBaseDataOrgAndRole;
import com.pubinfo.passbook.common.model.vo.persion.RabbitmqPersonConvertVO;
import com.pubinfo.passbook.common.service.dedicated.DedicatedService;
import com.pubinfo.passbook.common.service.master.TRabbitmqPersonConvertService;
import com.pubinfo.passbook.common.service.master.TRabbitmqPersonService;
import com.pubinfo.passbook.common.service.master.TWriteoffDataUserService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Slf4j
@Service
public class TWriteoffDataUserServiceImpl extends ServiceImpl<TWriteoffDataUserMapper, TWriteoffDataUser>
    implements TWriteoffDataUserService {
  
  @Autowired
  TRabbitmqPersonService personService;
  @Autowired
  DedicatedService dedicatedService;
  @Resource
  TRabbitmqPersonConvertService personConvertService;
  
  @SneakyThrows
  @Override
  public Long syncData() {
    //遍历person表，查询所有员工的账号
    PersonListParam params = new PersonListParam();
    params.setIsFilterOutsourcing(true);
    IPage<RabbitmqPersonConvertVO> convertVOIPage = personConvertService.searchPersonsByKeyword(params);
    Long totalPage = convertVOIPage.getPages();//总页数;
    for (long i = 1734; i < totalPage; i++) {
      params.setCurrent(i);
      IPage<RabbitmqPersonConvertVO> convertVOIPageItem = personConvertService.searchPersonsByKeyword(params);
      for (RabbitmqPersonConvertVO vo : convertVOIPageItem.getRecords()) {
        WriteoffBaseDataItem writeoffBaseDataItem = getUserAcc(vo.getAcct());
        if (writeoffBaseDataItem != null) {
          TWriteoffDataUser user = new TWriteoffDataUser(writeoffBaseDataItem);
          saveOrUpdate(user);
        }
      }
      // Thread.sleep(1000);
      log.info("当前页数：" + i);
    }
    return null;
  }
  
  public WriteoffBaseDataItem getUserAcc(String acct) {
    List<GetWriteoffBaseDataItems> account = new ArrayList<>();
    account.add(new GetWriteoffBaseDataItems(acct));
    List<WriteoffBaseDataItem> baseDataItems = this.dedicatedService.getWriteoffBaseDataItem(account).getItems();
    if (CollectionUtils.isEmpty(baseDataItems)) {
      //尝试使用@ZJ进行查询
      account.clear();
      account.add(new GetWriteoffBaseDataItems(acct + "@ZJ"));
      baseDataItems = this.dedicatedService.getWriteoffBaseDataItem(account).getItems();
    }
    if (!CollectionUtils.isEmpty(baseDataItems)) {
      List<WriteoffBaseDataItem> writeoffBaseDataItems = new ArrayList<>(baseDataItems);
      List<WriteoffBaseDataOrgAndRole> orgAndRoles = writeoffBaseDataItems.get(0).getWriteoffBaseDataOrgAndRoles();
      //如果只有一条,则直接返回,如果有多条,则查询4a数据,提取部门名称,关联后,再获取组织.
      if (orgAndRoles.size() > 1) {
        TRabbitmqPerson person = personService.selectByAcctId(acct);
        List<TbPerRLOrgRel> tbPerRLOrgRel = person.getTbPerRLOrgRel();
        if (!CollectionUtils.isEmpty(tbPerRLOrgRel)) {
          String fullOrgName = tbPerRLOrgRel.get(0).getFullOrgName();
          String orgName = fullOrgName.substring(fullOrgName.lastIndexOf("/") + 1);
          orgAndRoles = orgAndRoles.stream().filter(e -> e.getOrgName().equalsIgnoreCase(orgName)).collect(Collectors.toList());
        }
      }
      if (CollectionUtils.isEmpty(orgAndRoles)) {
        return baseDataItems.get(0);
      }
    }
    return null;
  }
}




